<?php
namespace app\api\controller;
/**
 * @authors leqing Cao (caoleqing@qq.com)
 * @date    2017-10-18 10:05:51
 * @link 	http://www.cnblogs.com/leqing/
 */

class User extends Common {
	/**
	 * 登录接口
	 * @return [json] [返回最终结果]
	 */
	public function login() {
		/*接收参数*/
		$data = $this->params;
		/*检测用户名*/
		$user_name_type = $this->check_username($data['user_name']);
		$where['user_isdel'] = 0;
		switch ($user_name_type) {
		case 'phone':
			$this->check_exites($data['user_name'], 'phone', 1);
			$where['user_phone'] = $data['user_name'];
			break;

		case 'email':
			$this->check_exites($data['user_name'], 'email', 1);
			$where['user_email'] = $data['user_name'];
			break;
		}

		$db_res = db('user')->where($where)->find();
		if ($db_res['user_pwd'] !== $data['user_pwd']) {
			$this->return_msg(400009, '用户名或密码错误');
		} else {
			unset($db_res['user_pwd']);
			$this->return_msg(200, '登陆成功', $db_res);
		}
	}
	/**
	 * 注册接口
	 * @return [json] [注册结果]
	 */
	public function register() {
		$insert_data = array();
		/*接受参数*/
		$data = $this->params;
		/*比对验证码*/
		$user_name = $data['user_name'];
		$mes_code = $data['mes_code'];
		$this->check_code($user_name, $mes_code);
		/*判断是手机还是邮箱*/
		$user_name_type = $this->check_username($user_name);
		switch ($user_name_type) {
		case 'phone':
			$info = db('user')->where('user_phone', $user_name)->find();
			if ($info) {
				$this->return_msg(400013, $user_name . '手机号已被注册');
			}
			$insert_data['user_phone'] = $user_name;
			break;

		case 'email':
			$info = db('user')->where('user_email', $user_name)->find();
			if ($info) {
				$this->return_msg(400013, $user_name . '邮箱已被注册');
			}
			$insert_data['user_email'] = $user_name;
			break;
		}
		/*存入数据库*/
		$insert_data['user_pwd'] = $data['user_pwd'];
		$insert_data['user_ctime'] = time();
		$res = db('user')->insert($insert_data);
		/*返回数据*/
		if (!$res) {
			$this->return_msg(400014, '用户注册失败');
		} else {
			$this->return_msg(200, '用户注册成功');
		}
	}
	/**
	 * 绑定手机或邮箱
	 * @return [json] [执行结果通知]
	 */
	public function bindUserInfo() {
		/*接受参数*/
		$data = $this->params;
		/*验证验证码是否正确*/
		$user_info = $data['user_info'];
		$que_code = $data['code'];
		$this->check_code($user_info, $que_code);
		/*验证是手机号还是邮箱*/
		$bind_type = $this->check_username($user_info);
		$bind_type_name = '';
		switch ($bind_type) {
		case 'phone':
			$bind_type_name = '手机号';
			$updata_data['user_phone'] = $user_info;
			break;

		case 'email':
			$bind_type_name = '邮箱';
			$updata_data['user_email'] = $user_info;
			break;
		}
		$res = db('user')->where('user_id', $data['user_id'])->update($updata_data);
		if (!$res) {
			$this->return_msg(400013, '绑定' . $bind_type_name . '失败,请稍后再试!');
		} else {
			$this->return_msg(200, $bind_type_name . '绑定成功!');
		}
	}
	/**
	 * 上传用户头像
	 * @return [json] [上传头像结果通知]
	 */
	public function upload_head_img() {
		/*①接受参数*/
		$data = $this->params;
		/*②上传文件*/
		$file_icon = $data['user_icon'];
		$head_icon_path = $this->upload_file($file_icon, 'head_img');
		/*③插入数据库*/
		$res = db('user')->where('user_id', $data['user_id'])->setField('user_icon', $head_icon_path);
		/*④结果返回*/
		if (!$res) {
			$this->return_msg(400015, '上传失败,请稍后再试');
		}
		$this->return_msg(200, '上传成功');
	}
	/**
	 * 修改用户昵称
	 * @return [json] [结果返回通知]
	 */
	public function chang_nickname() {
		/*1.接受参数*/
		$data = $this->params;
		/*2.确定昵称是否可用*/
		$res = db('user')->where('user_nickname', $data['user_nickname'])->find();
		if ($res) {
			$this->return_msg(400016, '该昵称已被占用');
		}
		/*3.存入数据库*/
		$res = db('user')->where('user_id', $data['user_id'])->setField('user_nickname', $data['user_nickname']);
		/*4.返回执行结果*/
		if (!$res) {
			$this->return_msg(400017, '服务器端发生错误');
		}
		$this->return_msg(200, '昵称修改成功');
	}
	/**
	 * 修改用户密码
	 * @return [json] [description]
	 */
	public function chang_password() {
		/*1.接受参数*/
		$data = $this->params;
		/*2.比对原密码是否正确*/
		$res = db('user')->where('user_id', $data['user_id'])->find();
		if ($res) {
			if ($res['user_pwd'] !== $data['old_pwd']) {
				$this->return_msg(400018, '原始密码不正确');
			}
		}
		/*4.存入数据库*/
		$res = db('user')->where('user_id', $data['user_id'])->setField('user_pwd', $data['new_pwd']);
		/*5.返回结果通知*/
		if (!$res) {
			$this->return_msg(400019, '密码修改失败');
		}
		$this->return_msg(200, '密码修改成功');
	}
	/**
	 * [find_password 找回密码]
	 * @return [json] [结果返回通知]
	 */
	public function find_password() {
		/*1.接收参数*/
		$data = $this->params;
		/*2.比对验证码*/
		$user_name = $data['user_name'];
		$code = $data['code'];
		$this->check_code($user_name, $code);
		/*3.判断是邮箱还是手机*/
		$user_name_type = $this->check_username($user_name);
		switch ($user_name_type) {
		case 'phone':
			$this->check_exites($user_name, 'phone', 1);
			$where['user_phone'] = $user_name;
			break;

		case 'email':
			$this->check_exites($user_name, 'email', 1);
			$where['user_email'] = $user_name;
			break;
		}
		/*4.存入数据库*/
		$res = db('user')->where($where)->setField('user_pwd', $data['user_pwd']);
		/*5.结果返回通知*/
		if ($res == false) {
			$this->return_msg(400021, '密码修改失败');
		}
		$this->return_msg(200, '密码修改成功');
	}
}